﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebCarERP.Models;

namespace WebCarERP.Controllers
{
    public class RepairListController : BaseController
    {
        //
        // GET: /OAVehicleRepair/

        public ActionResult Index()
        {
            ViewBag.Title = "修车清单";
            var info = (from x in db.RepairList.AsEnumerable().Where(t => t.Flag == 1)
                        //join y in db.SamplePersonInfo.AsEnumerable().Where(t => t.Flag == 1)
                        //on x.InfoId equals y.InfoId
                        select new RepairListModel
                        {
                            repairListNo = x.repairListNo,
                            carID = x.carID,
                            carType = x.carType,
                            company = x.company,
                            repairDate = x.repairDate,
                            InfoId = x.InfoId,
                            repairMaterial = x.repairMaterial,
                            unit = x.unit,
                            price = x.price,
                            materialCost = x.materialCost,
                            workingHour = x.workingHour,
                            brokerage = x.brokerage,
                            TotalRepairAmount = x.TotalRepairAmount,
                            repairParty = x.repairParty,
                            Flag = x.Flag,
                            addTime = x.addTime,
                            //Name = y.Name,
                            repairPartyMobile = x.repairPartyMobile,
                            Memo = x.Memo,
                            number = x.number,
                        });
            return View(info);
        }


        [HttpPost]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        public ActionResult add(RepairList entity)
        {
            entity.Flag = 1;
            entity.addTime = DateTime.Now;
            db.RepairList.Add(entity);
            int resultCount = db.SaveChanges();

            resultCount = db.SaveChanges();
            var info = (from x in db.RepairList.AsEnumerable().Where(t => t.Flag == 1 && entity.addTime == t.addTime)
                        //join y in db.SamplePersonInfo.AsEnumerable().Where(t => t.Flag == 1)
                        //on x.InfoId equals y.InfoId
                        select new RepairListModel
                        {
                            repairListNo = x.repairListNo,
                            carID = x.carID,
                            carType = x.carType,
                            company = x.company,
                            repairDate = x.repairDate,
                            InfoId = x.InfoId,
                            repairMaterial = x.repairMaterial,
                            unit = x.unit,
                            price = x.price,
                            materialCost = x.materialCost,
                            workingHour = x.workingHour,
                            brokerage = x.brokerage,
                            TotalRepairAmount = x.TotalRepairAmount,
                            repairParty = x.repairParty,
                            Flag = x.Flag,
                            addTime = x.addTime,
                            //Name = y.Name,
                            repairPartyMobile = x.repairPartyMobile,
                            Memo = x.Memo,
                            number = x.number,
                        });
            string json = JsonConvert.SerializeObject(info.FirstOrDefault());
            //data=Json( info.FirstOrDefault(),);
            return Content("{\"result\":\"添加成功\",\"data\":[" + json + "]}");
        }


        [HttpPost]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        public ActionResult edit(RepairList entity)
        {
            //entity.Flag = 1;

            //db.SamplePersonInfo.Add(entity);
            //int resultCount = db.SaveChanges();
            //entity2.InfoId = entity.InfoId;
            //entity2.Flag = 1;
            //db.SamplePersonInfoItem.Add(entity2);
            //resultCount = db.SaveChanges();
            RepairList RepairList = db.RepairList.Find(entity.repairListNo);
            RepairList.carID = entity.carID;
            RepairList.carType = entity.carType;
            RepairList.company = entity.company;
            RepairList.repairDate = entity.repairDate;
            RepairList.InfoId = entity.InfoId;
            RepairList.repairMaterial = entity.repairMaterial;
            RepairList.unit = entity.unit;
            RepairList.price = entity.price;
            RepairList.materialCost = entity.materialCost;
            RepairList.workingHour = entity.workingHour;
            RepairList.brokerage = entity.brokerage;
            RepairList.TotalRepairAmount = entity.TotalRepairAmount;
            RepairList.repairParty = entity.repairParty;
            //RepairList.Flag = entity.Flag;
            RepairList.addTime = DateTime.Now;
            //Name = y.Name;
            RepairList.repairPartyMobile = entity.repairPartyMobile;
            RepairList.Memo = entity.Memo;
            RepairList.number = entity.number;
            db.Entry(RepairList).State = EntityState.Modified;
            int resultCount = db.SaveChanges();
            var info = (from x in db.RepairList.AsEnumerable().Where(t => t.Flag == 1 && entity.addTime == t.addTime)
                        //join y in db.SamplePersonInfo.AsEnumerable().Where(t => t.Flag == 1)
                        //on x.InfoId equals y.InfoId
                        select new RepairListModel
                        {
                            repairListNo = x.repairListNo,
                            carID = x.carID,
                            carType = x.carType,
                            company = x.company,
                            repairDate = x.repairDate,
                            InfoId = x.InfoId,
                            repairMaterial = x.repairMaterial,
                            unit = x.unit,
                            price = x.price,
                            materialCost = x.materialCost,
                            workingHour = x.workingHour,
                            brokerage = x.brokerage,
                            TotalRepairAmount = x.TotalRepairAmount,
                            repairParty = x.repairParty,
                            Flag = x.Flag,
                            addTime = x.addTime,
                            //Name = y.Name,
                            repairPartyMobile = x.repairPartyMobile,
                            Memo = x.Memo,
                            number = x.number,
                        });
            string json = JsonConvert.SerializeObject(info.FirstOrDefault());
            //data=Json( info.FirstOrDefault(),);
            return Content("{\"result\":\"修改成功\",\"data\":[" + json + "]}");
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        public ActionResult delete(int? InfoId)
        {

            RepairList RepairList = db.RepairList.Find(InfoId);
            RepairList.Flag = 0;
            db.Entry(RepairList).State = EntityState.Modified;

            int resultCount = db.SaveChanges();

            //data=Json( info.FirstOrDefault(),);
            return Content("{\"result\":\"删除成功！\",\"data\":[]}");
        }

    }
}
